package com.htthorizon.model.service;

import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.utils.StringUtils;
import com.htthorizon.common.core.utils.ip.IpUtils;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.common.security.utils.SecurityUtils;
import com.htthorizon.system.api.domain.Config;
import com.htthorizon.model.mapper.ConfigMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Brilliant
 */
@Service
public class ConfigServiceImpl implements IConfigService{

    @Autowired
    private RedisService redisService;

    @Autowired
    private  ConfigMapper configMapper;

    @Override
    public Config getOne() {
      List<Config> list= configMapper.selectAll();
        return list.get(0);
    }

    private static final String IPADDRESS_REGEX="^((0|1\\d{0,2}|2[0-4]\\d|25[0-5])\\.){3}(0|1\\d{0,2}|[0-4]\\d|25[0-5])$";
    private static final Pattern IPADDRESS_PATTERN=Pattern.compile(IPADDRESS_REGEX);

    private static boolean validateIP(String ipAddress){
        Matcher matcher =IPADDRESS_PATTERN.matcher(ipAddress);
        return matcher.matches();
    }

    public static void main(String[] args) {
        String strIp="22.56.99.126";
        boolean ip = IpUtils.isIP(strIp);
        System.out.println(ip);
        System.out.println("validateIP(strIp) = " + validateIP(strIp));
    }


    @Override
    public boolean updateByPrimaryKey(Config config) {
        config.setId(1L);
        if (!(config.getMaxUserNum().equals(0)&& config.getMaxUserNum()==null)){
            redisService.setCacheObject(CacheConstants.MAX_SESSION_NUM_KEY, config.getMaxUserNum());
        }

        if (!Objects.isNull(config.getStartTime())){
            redisService.setCacheObject(CacheConstants.START_TIME_KEY, config.getStartTime());
        }

        if (!Objects.isNull(config.getEndTime())){
            redisService.setCacheObject(CacheConstants.END_TIME_KEY, config.getEndTime());
        }

        if (!Objects.isNull(config.getSessionTime())){
            redisService.setCacheObject(CacheConstants.SESSION_TIME_MAX, config.getSessionTime());
        }

        if (!Objects.isNull(config.getLogNum())){
            redisService.setCacheObject(CacheConstants.LOG_NUM,config.getLogNum());
        }
        if (StringUtils.isEmpty(config.getIp())){
            redisService.setCacheObject(CacheConstants.BLACK_IP,null);
        }
        if (!StringUtils.isEmpty(config.getIp())){
            String ips = config.getIp();
            String[] split = ips.split(",");
            for (String ip:split){
                if (!IpUtils.isIP(ip)) {
                    redisService.setCacheObject(CacheConstants.BLACK_IP,null);
                throw new RuntimeException("ip格式不对");
                }
            }
            redisService.setCacheObject(CacheConstants.BLACK_IP,config.getIp());

        }


        return configMapper.updateByPrimaryKey(config) > 0;
    }

}
